home *** CD-ROM | disk | FTP | other *** search
- /*
- File: AGSupprt.cpp
-
- Contains: xxx put contents here xxx
-
- Owned by: Yan Arrouye
-
- Copyright: © 1995-1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <2> 7/8/96 eeh undo task 10008 (AppleGuide buttons)
- <7> 5/16/96 Yan Style check
- <6> 5/09/96 Yan Fixed leak
- <5> 5/08/96 Yan fixed momory leaks
- <4> 4/25/96 Yan Use CopyPascalString instead of BlockMoveData
- <3> 2/20/96 tb Placed #if qDebug around TraceMonitor support.
- <2> 10/26/95 Yan • Added support for CodeWarrior. Requires that you rename the
- AppleGuideGlue.xcoff file to AppleguideGlueLib to link.
- 8/31/95 Yan Moved to ShareWare
-
- To Do:
- */
-
-
-
- #ifndef _AGSUPPORT_
- #include "AGSupprt.h"
- #endif
-
- #ifndef _DLGDEFS_
- #include "DdgDefs.h"
- #endif
-
- #ifndef _TEMPOBJ_
- #include "TempObj.h"
- #endif
-
- #ifndef _USERSRCM_
- #include <UseRsrcM.h>
- #endif
-
- #ifndef __CONTROLS__
- #include "Controls.h"
- #endif
-
- #ifndef __ICONS__
- #include "Icons.h"
- #endif
-
-
- //#include "Utilities.h"
-
- #if qDebug
- #include "TraceMonitor.h"
- #define kModuleChannel 'GUID'
- #endif
-
- #include <AppleGuide.h>
- #include <Balloons.h>
- #include <Gestalt.h>
- #include <Folders.h>
- #include <Files.h>
- #include <Menus.h>
- #include <TextUtils.h>
-
-
-
- #if GENERATING68K
- #pragma segment AppleGuide
- #endif
-
- /*******************************************************************************
- ** Globals
- *******************************************************************************/
- #define AG_BETA_RECEIVED
-
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma options align=mac68k
- #endif
-
- // Runtime structure
- typedef struct
- {
- FSSpec spec;
- AGRefNum ref;
- } InstalledGuideRec;
-
- typedef struct
- {
- short count;
- InstalledGuideRec guide[1];
- } InstalledGuideList, **InstalledGuideListHdl;
-
-
- // Resources
- typedef struct {
- Str32 name;
- char filler;
- Str255 itemTitle;
- } GuideEntry;
-
-
- typedef struct {
- short count;
- GuideEntry guide[1];
- } GuideList, **GuideListHdl;
-
-
-
- #if PRAGMA_ALIGN_SUPPORTED
- #pragma options align=reset
- #endif
-
-
-
- InstalledGuideListHdl gInstalledGuidesList = NULL;
- short gInstalledGuidesListSize = 0;
- short gFirstAppleGuideItem = 0;
- ODBoolean gAGInstallAttempted = kODFalse;
- ODBoolean gAGInstallSucceeded = kODFalse;
- FSSpec gFileSpec = { 0, 0, "\p" };
- unsigned long gRefNum = 0;
-
- /*******************************************************************************
- ** Boolean IsAppleGuidePresent
- *******************************************************************************/
-
- Boolean IsAppleGuidePresent()
- {
- long result = 0;
- OSErr err;
-
- err = Gestalt(gestaltHelpMgrAttr, &result);
-
- return ( err == noErr && (result & (1 << gestaltAppleGuidePresent)) != 0 );
- }
-
-
-
- /*******************************************************************************
- ** Boolean IsAppleGuideInstalled
- *******************************************************************************/
-
- Boolean IsAppleGuideInstalled()
- {
-
- return IsAppleGuidePresent() && gInstalledGuidesListSize != 0;
- }
-
-
-
- /*******************************************************************************
- ** void InstallGuide
- *******************************************************************************/
-
- static void FindAppleGuideFolder( short *vol, long *dir );
- static void FindAppleGuideFolder( short *vol, long *dir )
- {
- // Str255 folderName;
-
- CInfoPBRec pb;
- memset (&pb,0,sizeof(pb));
-
- //First find System Folder
- OSErr err = FindFolder(kOnSystemDisk, kExtensionFolderType,
- kDontCreateFolder, &pb.dirInfo.ioVRefNum, &pb.dirInfo.ioDrDirID );
-
- pb.dirInfo.ioNamePtr = (StringPtr)"\pGlobal Guide Files";
- pb.dirInfo.ioFDirIndex = 0;
- err = PBGetCatInfoSync( &pb );
-
- *vol = pb.dirInfo.ioVRefNum;
- *dir = pb.dirInfo.ioDrDirID;
- }
-
- void InstallAppleGuide()
- {
- if ( !gAGInstallAttempted && IsAppleGuidePresent() )
- {
- gAGInstallAttempted = kODTrue; // only try once -- so we aren't always failing
-
- short vol;
- long dir;
- FindAppleGuideFolder( &vol, &dir );
-
- OSErr err = FSMakeFSSpec( vol, dir, "\pOpenDoc Guide", &gFileSpec );
-
-
- #if 0 /* AG_BETA_RECEIVED */
- // Look in the Extensions folder the Macintosh guide and add it to the help menu as needed
- OSErr err;
- short vRefNum;
- long dirID;
-
- err = FindFolder(kOnSystemDisk, kExtensionFolderType, kDontCreateFolder,
- &vRefNum, &dirID);
-
- if ( err == noErr )
- {
- // If there is a guide specification, use it
- GuideListHdl guideList = (GuideListHdl)Get1Resource(kAppleGuidesList, 128);
-
- if ( guideList != NULL )
- {
- // There is a list of guides
- TempODHandleLock lock((Handle)guideList);
- MenuHandle helpMenu = NULL;
-
- short count = (**guideList).count;
- gInstalledGuidesList = (InstalledGuideListHdl)NewHandle(
- sizeof(InstalledGuideRec) * count );
-
- HMGetHelpMenuHandle(&helpMenu);
- if ( helpMenu != NULL )
- {
- gFirstAppleGuideItem = CountMItems(helpMenu) + 1;
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• No help menu\n");
- }
- #endif
-
- // Create the runtime list
- if ( gInstalledGuidesList != NULL )
- {
- short i;
- FSSpec spec;
-
- for ( i = 0; i < count; i++ )
- {
- err = FSMakeFSSpec(vRefNum, dirID,
- (**guideList).guide[i].name, &spec);
-
- if ( err == noErr )
- {
- gAGInstallSucceeded = kODTrue; // we've got at least one.
-
- // Guide found: stuff spec in list
- BlockMoveData(&spec,
- &(**gInstalledGuidesList).
- guide[gInstalledGuidesListSize].spec,
- sizeof(spec));
- (**gInstalledGuidesList).guide[gInstalledGuidesListSize]
- .ref = 0;
-
- #if qDebug
- Trace(kModuleChannel, kFatalMessageLevel, "• Adding guide '%*.*s', menu item '%*.*s'\n", (**guideList).guide[i].name[0], (**guideList).guide[i].name[0], (**guideList).guide[i].name + 1, (**guideList).guide[i].itemTitle[0], (**guideList).guide[i].itemTitle[0], (**guideList).guide[i].itemTitle + 1);
- #endif
-
- // Install guide in menu
- if ( helpMenu != NULL )
- {
- AppendMenu(helpMenu,
- (**guideList).guide[i].itemTitle);
- }
-
- // Increment installed guides list list index
- gInstalledGuidesListSize++;
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• Error %d creating spec for guide '%*.*s'\n", err, (**guideList).guide[i].name[0], (**guideList).guide[i].name[0], (**guideList).guide[i].name + 1);
- }
- #endif
- }
-
- // Adjust list size
- SetHandleSize((Handle)gInstalledGuidesList,
- gInstalledGuidesListSize * sizeof(InstalledGuideRec) );
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• Can't allocate installed guides list\n", err);
- }
- #endif
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• No 'AGd#' id 128 resource\n", err);
- }
- #endif
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• Error %d finding extensions folder\n", err);
- }
- #endif
- #endif /* AG_BETA_RECEIVED */
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
- }
- #endif
- }
-
-
-
- /*******************************************************************************
- ** void CloseAppleGuide
- *******************************************************************************/
-
- void CloseAppleGuide()
- {
- #ifdef AG_BETA_RECEIVED
- if ( IsAppleGuidePresent() == true )
- {
- short i;
-
- for ( i = 0; i < gInstalledGuidesListSize; i++ )
- {
- AGRefNum ref = (**gInstalledGuidesList).guide[i].ref;
-
- if ( ref != 0 && AGIsDatabaseOpen(ref) == true )
- {
- // Guide already open
- AGClose(&ref);
- (**gInstalledGuidesList).guide[i].ref = 0;
- }
- }
-
- if ( gInstalledGuidesList )
- {
- DisposeHandle((Handle)gInstalledGuidesList);
- gInstalledGuidesList = NULL;
- }
- AGQuit();
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
- }
- #endif
- #endif /* AG_BETA_RECEIVED */
- }
-
-
-
-
- /*******************************************************************************
- ** Boolean OpenThisGuide
- *******************************************************************************/
-
- static Boolean OpenThisGuide(FSSpec* guide, AGRefNum* refNum,
- StringPtr keyword = NULL)
- {
- Boolean guideOpened = false;
-
- if ( IsAppleGuidePresent() )
- {
- if ( AGIsDatabaseOpen(*refNum) == true )
- {
- // Guide already open
- AGClose(refNum);
- *refNum = 0;
- }
-
- #if qDebug
- Trace(kModuleChannel, kFatalMessageLevel,
- "•• Looking for keyword '%*.*s'\n", keyword[0], keyword[0],
- keyword+1);
- #endif
-
- // Open AppleGuide
- if ( guide->name[0] != 0 )
- {
- AGErr err;
-
- // Open AppleGuide
- if ( keyword != NULL && keyword[0] != 0 )
- {
- // Open using the specified keyword
- err = AGOpenWithSearch( guide, 0, NULL, keyword, refNum);
- }
- else
- {
- // Open root window
- err = AGOpenWithView( guide, 0, NULL, kAGViewFullHowdy, refNum);
- }
-
- if ( err == noErr )
- {
- guideOpened = true;
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "••• AppleGuide opening error %d\n", err);
- }
- #endif
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• No guide spec!\n");
- }
- #endif
- }
- #if qDebug
- else
- {
- Trace(kModuleChannel, kFatalMessageLevel, "•• AppleGuide not installed\n");
- }
- #endif
-
- return guideOpened;
- }
-
-
- /*******************************************************************************
- ** Boolean OpenAppleGuide
- *******************************************************************************/
-
- Boolean OpenAppleGuide(StringPtr keyword)
- {
- Boolean opened = false;
- #ifdef AG_BETA_RECEIVED
- if ( (gInstalledGuidesListSize > 0) || (gFileSpec.name[0] != 0) )
- {
- TempODHandleLock list((Handle)gInstalledGuidesList);
-
-
- // opened = OpenThisGuide( kAGDefault, &gRefNum, keyword );
- opened = OpenThisGuide( &gFileSpec, &gRefNum, keyword );
-
- // opened = OpenThisGuide( &(**gInstalledGuidesList).guide[0].spec,
- // &(**gInstalledGuidesList).guide[0].ref, keyword);
- }
- #endif /* AG_BETA_RECEIVED */
- return opened;
- }
-
- /*******************************************************************************
- ** Boolean OpenAppleGuideMenuItem
- *******************************************************************************/
-
- #if 0
- Boolean OpenAppleGuideMenuItem(short item)
- {
- Boolean opened = false;
-
- if ( IsAppleGuidePresent() )
- {
- TempODHandleLock list((Handle)gInstalledGuidesList);
-
- opened = OpenThisGuide((**gInstalledGuidesList).guide[item - gFirstAppleGuideItem].spec, (**gInstalledGuidesList).guide[item - gFirstAppleGuideItem].ref);
- }
-
- return opened;
- }
- #endif
-
- /*******************************************************************************
- ** InitAppleGuideSupport
- *******************************************************************************/
- //short gAGButtonItem = -1;
- ControlDefUPP gCtrlDefUPP = NULL;
- Handle ctrlDefStub = NULL;
- Handle gIconSuite = NULL;
-
- const short kControlInactive = 255;
- const short kControlActive = 0;
- const short kCDEFAddrOffset = 8;
-
- pascal long AGBUTTONCDEF( short code, ControlHandle ctrlH,
- short message, long param );
-
- void InitAppleGuideSupport()
- {
- CUsingLibraryResources r;
- InstallAppleGuide(); // returns immediately if installed
-
- // Must fix up the CDEF resource whether the install succeeded
- // or not; else we'll jsr to 0.
- if ( !gCtrlDefUPP )
- {
- gCtrlDefUPP = NewControlDefProc( AGBUTTONCDEF );
- ctrlDefStub = Get1Resource( 'CDEF', kAGButtonCDEFId);
- WASSERT( ctrlDefStub != NULL );
- (*(ControlDefUPP*)&((*ctrlDefStub)[kCDEFAddrOffset])) = gCtrlDefUPP;
- }
- }
-
- /*******************************************************************************
- ** TakedownAppleGuideSupport
- *******************************************************************************/
-
- void TakedownAppleGuideSupport()
- {
- if ( gCtrlDefUPP )
- {
- DisposeRoutineDescriptor( gCtrlDefUPP );
- gCtrlDefUPP = NULL;
- }
- if ( ctrlDefStub )
- {
- CUsingLibraryResources r; // <eeh> find out if necessary
- ReleaseResource( ctrlDefStub );
- ctrlDefStub = NULL;
- }
- if ( gIconSuite )
- {
- DisposeIconSuite( gIconSuite, true );
- gIconSuite = NULL;
- }
- CloseAppleGuide();
- }
-
- /*******************************************************************************
- ** TakedownAppleGuideSupport
- *******************************************************************************/
-
- void DialogSetUpAppleGuide( DialogPtr dlg, short item )
- {
- // gAGButtonItem = item;
-
- short itemType;
- ControlHandle cntrlHandle;
- Rect scratchRect;
- GetDialogItem(dlg, item, &itemType, (Handle*)&cntrlHandle,
- &scratchRect );
-
- // <eeh> don't check this in!!!!
- gAGInstallSucceeded = true;
-
- HiliteControl( cntrlHandle,
- gAGInstallSucceeded? kControlActive : kControlInactive);
-
- if ( gIconSuite == NULL )
- {
- CUsingLibraryResources r;
- short iconsID = (*cntrlHandle)->contrlRfCon;
- // ignore error; gIconSuite *should* remain null.
- OSErr err = GetIconSuite( &gIconSuite, iconsID,
- kSelectorAllSmallData );
- if ( err != noErr )
- {
- gIconSuite = NULL;
- WARN( "GetIconSuite returned null" );
- }
- }
- }
-
- IconTransformType gCurrentTransform = kTransformNone;
-
- void Plot( ControlHandle ctrlH, IconTransformType trans );
- void Plot( ControlHandle ctrlH, IconTransformType trans )
- {
- if ( gIconSuite )
- {
- OSErr err = PlotIconSuite( &(*ctrlH)->contrlRect,
- kAlignNone, gCurrentTransform=trans,
- (Handle)gIconSuite );
- WASSERT( err == noErr );
- }
- }
-
- pascal long AGBUTTONCDEF( short code, ControlHandle ctrlH,
- short message, long param )
- {
- long result = 0;
- switch ( message )
- {
- case drawCntl:
- if ( (*ctrlH)->contrlVis /* == 255 visible */ )
- {
- short activeCode = (*ctrlH)->contrlHilite;
- switch ( activeCode )
- {
- case kControlInactivePart:
- Plot( ctrlH, kTransformDisabled );
- break;
- case kControlButtonPart:
- Plot( ctrlH, kTransformSelected );
- break;
- default:
- Plot( ctrlH, kTransformNone );
- }
- }
- break;
- case testCntl:
- Point pt = *(Point*)¶m;
- if ( PtInRect( pt, &(*ctrlH)->contrlRect ) )
- {
- result = kControlButtonPart;
- if ( gCurrentTransform == kTransformNone )
- Plot( ctrlH, kTransformSelected );
- }
- break;
-
- // we could use these rather than the methods above to do ifam init
- // case initCntl:
- // case dispCntl:
- // break;
-
- // case calcCRgns: // not an issue for OpenDoc
- case calcCntlRgn:
- param &= 0x7fffffff; // clear high bit (NIM 5-112)
- RectRgn( (RgnHandle)param, &(*ctrlH)->contrlRect );
- break;
-
- }
- return result;
- }